<?php
namespace app\common\model;

/**
 * 签到记录模型
 */
class SignLog extends BaseModel
{
    // 定义表名
    protected $name = 'sign_log';

    // 定义主键
    protected $pk = 'sign_log_id';

    // 追加字段
    protected $append = [
        'user'
    ];
    
    /**
     * 用户信息
     */
    public function getUserAttr($value,$data)
    {
        return User::withoutGlobalScope()->where('user_id',$data['user_id'])->find();
    }
    /**
     * 奖励类型
     */
    public function getTypeAttr($value)
    {
        $status = ['score' => '积分', 'money' => '余额'];
        return ['text' => $status[$value], 'value' => $value];
    }
    /**
     * 获取列表
     */
    public function getList($user_id = 0)
    {
        // 筛选条件
        $filter = [];
        $user_id > 0 && $filter['user_id'] = $user_id;
        // 执行查询
        $list = $this->where($filter)
            ->order('sign_log_id','desc')
            ->paginate(['list_rows'=>15,'query' => request()->param()]);
        return $list;
    }
    
    /**
     * 获取今天签到详情
     */
    public function getIsSign($user_id)
    {
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        return $this->where('user_id',$user_id)->where('create_time','>',$beginToday)->count();
    }
}
